package com.ccit.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccit.common.utils.UuidUtil;
import com.ccit.model.Suggestion;
import com.ccit.order.mapper.SuggestionMapper;
import com.ccit.order.service.SuggestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class SuggestionServiceImpl implements SuggestionService {
    @Autowired
    private SuggestionMapper suggestionMapper;

    @Override
    public void addSuggestion(Suggestion suggestion) {
        suggestion.setUuid(UuidUtil.getUuid());
        suggestion.setTime(LocalDateTime.now());
        suggestion.setStatus("未处理");
        suggestionMapper.insert(suggestion);
    }

    @Override
    public void deleteSuggestion(String uuid) {
        suggestionMapper.deleteById(uuid);
    }

    @Override
    public void updateSuggestion(Suggestion suggestion) {
        suggestionMapper.updateById(suggestion);
    }

    @Override
    public Suggestion getSuggestion(String uuid) {
        return null;
    }

    @Override
    public List<Suggestion> getAll() {
        return Collections.emptyList();
    }

    @Override
    public IPage<Suggestion> getByPhone(String phone, Integer pageNum, Integer pageSize, String status) {
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        QueryWrapper<Suggestion> wrapper = new QueryWrapper<>();
        wrapper.eq("status", status);
        if (phone != null && !phone.isEmpty()) {
            wrapper.eq("phone", phone);
        }
        Page<Suggestion> page = new Page<>(pageNum, pageSize);
        return suggestionMapper.selectPage(page, wrapper);
    }

    @Override
    public IPage<Suggestion> getByPage(Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        Page<Suggestion> page = new Page<>(pageNum, pageSize);
        return suggestionMapper.selectPage(page, null);
    }
}
